﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 24.04.2008
 * Zeit: 15:37
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;

namespace Gamadus.Core.Resources
{
	/// <summary>
	/// A collection of other groups or resources
	/// </summary>
	public class Group : Node
	{
		List<Node> nodes = new List<Node>();
		Resources.Type m_type;
		bool isRoot;
		public bool IsRoot
		{
			get
			{
				return isRoot;
			}
		}
		
		public Resources.Type Type
		{
			get
			{
				return m_type;
			}
		}
		
		public string Name
		{
			get
			{
				return m_name;
			}
			set
			{
				m_name = value;
			}
		}
		
		/// <summary>
		/// Creates a new sub group
		/// </summary>
		/// <param name="name">The resource type of the group</param>
		public Group(string name, Resources.Type type) : base(name)
		{
			m_type = type;
		}
		
		/// <summary>
		/// Creates a new group root-level group
		/// </summary>
		/// <param name="type">The resource type of the group</param>
		public Group(Resources.Type type) : base(type.ToString()+"Root")
		{
			m_type = type;
			isRoot = true;
		}
		
		public void AddGroup(string name)
		{
			nodes.Add(new Group(name, m_type));
		}
		
		public void AddNode(Node node)
		{
			nodes.Add(node);
		}
		
		public bool ResourceNameExists(string name)
		{
			return ResourceNameExists(name, null);
		}
		
		public bool ResourceNameExists(string name, Resource skip)
		{
			foreach(Node node in nodes)
			{
				if(node is Resource && (node as Resource).Name == name && node != skip)
					return true;
				else if(node is Group && (node as Group).ResourceNameExists(name, skip))
					return true;
			}
			return false;
		}
	}
	
	/// <summary>
	/// A node of a resource group, either a group or a resource
	/// </summary>
	public class Node
	{
		protected string m_name;
		
		public Node(string name)
		{
			m_name = name;
		}
		
		public Node()
		{
			m_name = "Unnamed";
		}
	}
}
